﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 8/29/2009
 * Time: 12:55 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Buaya.Model;
using Buaya.Util;
using System.IO;

namespace Buaya.Gui
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : BaseForm
	{
		static MainForm instance = new MainForm();
		
		public static MainForm Instance {
			get { return instance; }
			set { instance = value; }
		}
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			AttachCommands();
			Text = ApplicationUtility.ProductNameAndVersion;
		}
		
		void SetupUserAccess()
		{
			foreach (UserModule m in UserSingleton.Instance.Modules) {
				ToolStripMenuItem moduleItem = new ToolStripMenuItem();
				moduleItem.Text = m.Module.Name;
				menuStrip1.Items.Add(moduleItem);
				foreach (UserModuleCommand c in m.Commands) {
					ToolStripItem commandItem = null;
					if (c.ModuleCommand.Name == "-") {
						commandItem = new ToolStripSeparator();
					} else {
						commandItem = new ToolStripMenuItem();
					}
					commandItem.Text = c.ModuleCommand.Name;
					ICommand command = c.CreateCommandInstance(); 
					if (command != null) {
						commandItem.Click += delegate { command.Run(); };
					}
					moduleItem.DropDownItems.Add(commandItem);
				}
			}
		}
		
		public void ToggleWindowListPane()
		{
			openWindowListPane1.Visible = openWindowListToolStripMenuItem.Checked = !openWindowListToolStripMenuItem.Checked;
		}
		
		public void ToggleIconBar()
		{
			toolStrip1.Visible = iconBarToolStripMenuItem.Checked = !iconBarToolStripMenuItem.Checked;
		}
		
		void AttachCommands()
		{
			// File
			logOffToolStripMenuItem.Click += delegate { new Commands.LogOff().Run(); };
			exitToolStripMenuItem.Click += delegate { new Commands.ExitCommand().Run(); };
			
			// Edit
			useCalculatorToolStripMenuItem.Click += delegate { new Commands.UseCalculator().Run(); };
			preferencesToolStripMenuItem.Click += delegate { new Commands.ShowPreferences().Run(); };
			
			// View
			openWindowListToolStripMenuItem.Click += delegate { new Commands.ToggleWindowListPane().Run(); };
			iconBarToolStripMenuItem.Click += delegate { new Commands.ToggleIconBar().Run(); };
			dailyTimeRecorderToolStripMenuItem.Click += delegate { new Commands.ViewDailyTimeRecorder().Run(); };
			
			// Company
			homePageToolStripMenuItem.Click += delegate { new Commands.ShowHomePage().Run(); };
			companyInformationToolStripMenuItem.Click += delegate { new Commands.ShowCompanyInformation().Run(); };
			setupUsersToolStripMenuItem.Click += delegate { new Commands.ListUsers().Run(); };
			changePasswordToolStripMenuItem.Click += delegate { new Commands.ChangePassword().Run(); };
			toDoListToolStripMenuItem.Click += delegate { AddChild(new ToDoListForm()); };
			
			// Employees
			createDeductionsToolStripMenuItem.Click += delegate { new Commands.CreateDeductions().Run(); };
			createLoansToolStripMenuItem.Click += delegate { new Commands.CreateLoans().Run(); };
			enterTimesheetsToolStripMenuItem.Click += delegate { new Commands.EnterTimesheets().Run(); };
			processPayrollToolStripMenuItem.Click += delegate { new Commands.ProcessPayroll().Run(); };
			employeeListToolStripMenuItem.Click += delegate { new Commands.ListEmployees().Run(); };
			departmentListToolStripMenuItem.Click += delegate { new Commands.ListDepartments().Run(); };
			positionListToolStripMenuItem.Click += delegate { new Commands.ListPositions().Run();; };
			employeeTypeListToolStripMenuItem.Click += delegate { new Commands.ListEmployeeTypes().Run(); };
			loanTypeListToolStripMenuItem.Click += delegate { new Commands.ListLoanTypes().Run(); };
			deductionTypeListToolStripMenuItem.Click += delegate { new Commands.ListDeductionTypes().Run(); };
			workersCompensationListToolStripMenuItem.Click += delegate { new Commands.ListWorkersCompensations().Run(); };
			sSSPremiumListToolStripMenuItem.Click += delegate { new Commands.ListSSSPremiums().Run(); };
			pagIBIGPremiumListToolStripMenuItem.Click += delegate { new Commands.ListPagIBIGPremiums().Run(); };
			philhealthPremiumListToolStripMenuItem.Click += delegate { new Commands.ListPhilhealthPremiums().Run(); };
			taxSalaryCodesToolStripMenuItem.Click += delegate { new Commands.ListTaxCodes().Run(); };
			shiftListToolStripMenuItem.Click += delegate { new Commands.ListShifts().Run(); };
			payrollCodeListToolStripMenuItem.Click += delegate { new Commands.ListPayrollCodes().Run(); };
			bankListToolStripMenuItem.Click += delegate { new Commands.ListBanks().Run(); };
			timesheetListToolStripMenuItem.Click += delegate { new Commands.ListTimesheets().Run(); };
			
			// Reports
			employeeListToolStripMenuItem1.Click += delegate { new Commands.ShowEmployeeListReport().Run(); };
			departmentListToolStripMenuItem1.Click += delegate { new Commands.ShowDepartmentListReport().Run(); };
			
			// Window
			arrangeIconsToolStripMenuItem.Click += delegate { new Commands.ArrangeIcons().Run(); };
			closeAllToolStripMenuItem.Click += delegate { new Commands.CloseAllChildren().Run(); };
			tileHorizontallyToolStripMenuItem.Click += delegate { new Commands.TileHorizontally().Run(); };
			tileVerticallyToolStripMenuItem.Click += delegate { new Commands.TileVertically().Run(); };
			cascadeToolStripMenuItem.Click += delegate { new Commands.Cascade().Run(); };
			
			// ?
			codeplexDiscussionsToolStripMenuItem.Click += delegate { new Commands.VisitUserCommunity().Run(); };
			codeplexIssueTrackerToolStripMenuItem.Click += delegate { new Commands.VisitIssueTracker().Run(); };
			aboutToolStripMenuItem.Click += delegate { new Commands.ShowAbout().Run(); };
		}
		
		[System.ComponentModel.EditorBrowsableAttribute()]
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			this.Visible = true;
			Login();
		}
		
		void Login()
		{
			menuStrip1.Enabled = toolStrip1.Visible = statusStrip1.Visible = openWindowListPane1.Visible = false;
			using (LoginForm f = new LoginForm()) {
//				f.UserChanged += delegate { SetupUserAccess(); };
				if (AddDialog(f) == DialogResult.Cancel) {
					Close();
				} else {
					menuStrip1.Enabled = toolStrip1.Visible = statusStrip1.Visible = openWindowListPane1.Visible = true;
					this.IsMdiContainer = true;
					homePageToolStripMenuItem.PerformClick();
				}
			}
		}
		
		public void LogOff()
		{
//			menuStrip1.Items.Clear();
			CloseAllChildren(); Login();
		}
		
		public void CloseAllChildren()
		{
			foreach (Form f in this.MdiChildren) {
				f.Close();
			}
		}
		
		public void AddChild(Form form, string text)
		{
			form.Text = text;
			AddChild(form);
		}
		
		public void AddChild(Form form)
		{
			form.MdiParent = this;
			form.WindowState = FormWindowState.Maximized;
			openWindowListPane1.AddForm(form);
			form.Show();
		}
		
		public DialogResult AddDialog(Form form)
		{
			form.StartPosition = FormStartPosition.CenterParent;
			form.FormBorderStyle = FormBorderStyle.FixedDialog;
			form.KeyPreview = true;
			form.MinimizeBox = form.MaximizeBox = form.ShowInTaskbar = false;
			form.KeyDown += delegate(object sender, KeyEventArgs e) { 
				if (e.KeyCode == Keys.Escape) {
					form.Close();
				}
			};
			return form.ShowDialog(this);
		}
	}
}
